<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Bill\Controllers;


use App\Eloquent\Ygt\BillLog;
use App\Eloquent\Ygt\BillWage;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class WageController extends Controller
{
    public $userId          = 25;
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $year               = request('year',0);
        $month              = request('month',0);
        if( !$userInfo )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $nowTime            = $_SERVER['REQUEST_TIME'];
        $nowYear            = date('Y',$nowTime);
        $nowMonth           = date('n',$nowTime);
        $year               = $year ? $year : $nowYear;
        $month              = $month ? $month : $nowMonth;
        $startTime          = $year.'-'.$month.'-1';
        $monthTimeInt       = strtotime($startTime);
        $monthDays          = date('t',$monthTimeInt);
        $endTime            = $year.'-'.$month.'-'.$monthDays;
        $emptyResult        = false;
        if($year > $nowYear){
            $emptyResult    = true;
        }else{
            if($month > $nowMonth)
            {
                $emptyResult    = true;
            }
        }

        //////////////////////////////////////////////////////////////////////////////
        ///员工工资记录数据库,N 种情况
        ///第一种情况,当前月无记录,工单统计无数据,直接返回空
        ///第二种情况,当前月无记录,工单统计有数据,直接插入
        ///第三种情况,当前月有记录,工单统计无数据[员工有预支,没做工]
        ///第四种情况,当前月有记录,工单统计有数据,更新对应员工的工资金额
        ///第五种情况,非前月无记录,工单统计无数据,直接返回空
        ///第六种情况,非前月无记录,工单统计有数据,直接插入
        ///第七种情况,非前月有记录,工单统计有数据,取记录数据忽略工单统计数据
        //////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////
        ///员工工资金额等不记录数据库,
        ///     工单数量,累计数量,工资金额由工单统计为准
        ///     预支,已付,是否结清记录数据为准
        /// 第一种工单统计有数据,记录无数据,返回空
        /// 第二种工单统计有数据,记录有数据,合并
        /// 第三种工单统计有数据,记录无数据,合并[员工有预支,没做工][极限情况]
        $lists              = [];
        $monyPay            = $monyBorrow = $moneyBalance = 0;
        if(!$emptyResult)
        {
            $userWageList       = \App\Engine\OrderEngine::getCompanyStaffWorkInfo($companyId,$startTime,$endTime);
            if(!empty($userWageList))
            {
                foreach($userWageList as $key=>$val)
                {
                    $temp           = [
                        'user_id'=>$val['uid'],
                        'truename'=>$val['true_name'],
                        'order_number'=>$val['order_num'],
                        'number'=>$val['submit_num'],
                        'all_money'=>$val['wages'],
                        'mony_pay'=>0,
                        'money_borrow'=>0,
                        'status'=>0,
                    ];
                    $lists[]        = $temp;
                }
            }
            $where              = ['company_id'=>$companyId,'year'=>$year,'month'=>$month];
            $billWageList       = BillWage::getList($where)->toArray();
            if(!empty($billWageList) && !empty($lists))
            {
                foreach($billWageList as $k=>$v)
                {
                    $monyPay        += $v['mony_pay'];
                    $monyBorrow     += $v['money_borrow'];
                }
                $moneyBalance       = $monyPay + $monyBorrow;
                foreach($lists as $key=>$val)
                {
                    foreach($billWageList as $k=>$v)
                    {
                        if($val['user_id'] == $v['user_id'])
                        {
                            $lists[$key]['mony_pay']        = $v['mony_pay'];
                            $lists[$key]['money_borrow']   = $v['money_borrow'];
                            $lists[$key]['status']          = $v['status'];
                        }
                    }
                }
            }
        }
        $totalMoney     = 0;
        $data           = [
            'lists'=>$lists,
            'money_pay'=>$monyPay,
            'money_borrow'=>$monyBorrow,
            'money_balance'=>$moneyBalance,
            'total_money'=>$totalMoney,
            'month'=>$month.'月',
            //'year'=>$year
        ];
        $pagingData         = [
            'page'         => 1,
            'pageSize'     => 10,
            'total'         => 1,
            'total_page'   => 1,
            'keyword'      => ''
        ];
        return $this->pagingData($data, $pagingData);
    }
    public function moneyList()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $tableId            = request( 'id', 0 );
        if( !$userInfo || !$tableId )
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = [
            'ygt_bill_log.type'=>3,
            'ygt_bill_log.table'=>'user',
            'ygt_bill_log.table_id'=>$tableId
        ];
        $limit              = $offset = '';
        $lists              = BillLog::getListDetail($where,$limit,$offset);
        return $lists;
    }
    public function orderList()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $typeId             = request( 'id', 0 );
        $year               = request( 'year', 0 );
        $month              = request( 'month', 0 );
        if( !$userInfo || !$typeId )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $nowTime            = $_SERVER['REQUEST_TIME'];
        $nowYear            = date('Y',$nowTime);
        $nowMonth           = date('n',$nowTime);
        $year               = $year ? $year : $nowYear;
        $month              = $month ? $month : $nowMonth;
        $startTime          = $year.'-'.$month.'-1';
        $monthTimeInt       = strtotime($startTime);
        $monthDays          = date('t',$monthTimeInt);
        $endTime            = $year.'-'.$month.'-'.$monthDays;
        $sourceResult       = true;
        if($year > $nowYear)
        {
            $sourceResult   = false;
        }
        if($month > $nowMonth)
        {
            $sourceResult   = false;
        }
        $orderSource        = [];
        if($sourceResult)
        {
            $orderSource        = \App\Engine\OrderEngine::getStaffOrderList($companyId,$typeId,$startTime,$endTime);
        }
        $result             = [
            'pageID'=>0,
            'data'=>['tableviewSource'=>$orderSource]
        ];
        return $result;
    }
}